package com.qixin.modules.eaStandard.service.impl;

import com.qixin.modules.eaBasic.util.ChangeIdToNameUtil;
import com.qixin.modules.eaStandard.entity.EaShipstandard;
import com.qixin.modules.eaStandard.mapper.EaShipstandardMapper;
import com.qixin.modules.eaStandard.service.IEaShipstandardService;
import org.jeecg.common.util.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 轮船标准
 * @Author: qixin-boot
 * @Date:   2021-07-17
 * @Version: V1.0
 */
@Service
public class EaShipstandardServiceImpl extends ServiceImpl<EaShipstandardMapper, EaShipstandard> implements IEaShipstandardService {
    @Autowired
    private EaShipstandardMapper eaShipstandardMapper;
    @Autowired
    private ChangeIdToNameUtil changeIdToNameUtil;

    /**
     * 保存
     * @param eaShipstandard
     * @return map
     */
    public Map<String,Object> saveObject(EaShipstandard eaShipstandard){
        Map<String,Object> returnMap = new HashMap<String,Object>();

        //判断是否已经存在(相应的用户标准/相应的部门-职务)报销控制标准
        String relationtype = eaShipstandard.getCompanycode();   //关系类型:1用户关联,0部门职务关联
        String personnelid = EmptyUtil.checkNull(eaShipstandard.getPersonnelid());    //用户
        String dept = EmptyUtil.checkNull(eaShipstandard.getDept());                  //部门
        String titleid = EmptyUtil.checkNull(eaShipstandard.getTitleid());            //职务
        String rankid = EmptyUtil.checkNull(eaShipstandard.getRankid());              //职级

        //查询报销控制标准
        if(relationtype.equals("1")){   //按用户关联
            Map<String,Object> params = new HashMap<String,Object>();
            params.put("personnelid",personnelid);
            List<EaShipstandard> eaShipstandardList = eaShipstandardMapper.selectByMap(params);
            if(eaShipstandardList.size() > 0){
                returnMap.put("code","-1");
                returnMap.put("message","用户:" + changeIdToNameUtil.getNameById(personnelid) + "已经维护报销控制标准,不要重复维护!");
                return returnMap;
            }else{
                eaShipstandardMapper.insert(eaShipstandard);

                returnMap.put("code","0");
                returnMap.put("message","新增成功!");
                return returnMap;
            }
        }else{  //部门职务关联
            Map<String,Object> params = new HashMap<String,Object>();
            params.put("dept",dept);
            params.put("rankid",rankid);
//            params.put("titleid",titleid);
            List<EaShipstandard> eaShipstandardList = eaShipstandardMapper.selectByMap(params);
            if(eaShipstandardList.size() > 0){
                returnMap.put("code","-1");
                returnMap.put("message","部门/职务(" + changeIdToNameUtil.getNameByDepartId(dept) + "/" + changeIdToNameUtil.getNameByTitleId(titleid) + ")已经维护报销控制标准,不要重复维护!");
                return returnMap;
            }else{
                eaShipstandardMapper.insert(eaShipstandard);

                returnMap.put("code","0");
                returnMap.put("message","新增成功!");
                return returnMap;
            }
        }
    }

    /**
     * 编辑
     * @param eaShipstandard
     * @return map
     */
    public Map<String,Object> updateByIdObject(EaShipstandard eaShipstandard){
        Map<String,Object> returnMap = new HashMap<String,Object>();

        //判断是否已经存在(相应的用户标准/相应的部门-职务)报销控制标准
        String relationtype = eaShipstandard.getCompanycode();   //关系类型:1用户关联,0部门职务关联
        String personnelid = EmptyUtil.checkNull(eaShipstandard.getPersonnelid());    //用户
        String dept = EmptyUtil.checkNull(eaShipstandard.getDept());                  //部门
        String titleid = EmptyUtil.checkNull(eaShipstandard.getTitleid());            //职务
        String rankid = EmptyUtil.checkNull(eaShipstandard.getRankid());              //职级

        //查询报销控制标准
        if(relationtype.equals("1")){   //按用户关联
            Map<String,Object> params = new HashMap<String,Object>();
            params.put("personnelid",personnelid);
            List<EaShipstandard> eaShipstandardList = eaShipstandardMapper.selectByMap(params);
            if(eaShipstandardList.size() > 0){
                //查询当前标准
                EaShipstandard eaShipstandard1 = eaShipstandardMapper.selectById(eaShipstandard.getId());
                if(eaShipstandard1.getPersonnelid().equals(personnelid)){  //如果当前标准维护的用户没变,返回成功
                    eaShipstandardMapper.updateById(eaShipstandard);

                    returnMap.put("code","0");
                    returnMap.put("message","编辑成功!");
                    return returnMap;
                }else{
                    returnMap.put("code","-1");
                    returnMap.put("message","用户:" + changeIdToNameUtil.getNameById(personnelid) + "已经维护报销控制标准,不要重复维护!");
                    return returnMap;
                }
            }else{
                eaShipstandardMapper.updateById(eaShipstandard);

                returnMap.put("code","0");
                returnMap.put("message","编辑成功!");
                return returnMap;
            }
        }else{  //部门职务关联
            Map<String,Object> params = new HashMap<String,Object>();
            params.put("dept",dept);
            params.put("rankid",rankid);
//            params.put("titleid",titleid);
            List<EaShipstandard> eaShipstandardList = eaShipstandardMapper.selectByMap(params);
            if(eaShipstandardList.size() > 0){
                //查询当前标准
                EaShipstandard eaShipstandard1 = eaShipstandardMapper.selectById(eaShipstandard.getId());
                if(eaShipstandard1.getDept().equals(dept) && eaShipstandard1.getRankid().equals(rankid)){  //如果当前标准维护的(单位/职务)没变,返回成功
                    eaShipstandardMapper.updateById(eaShipstandard);

                    returnMap.put("code","0");
                    returnMap.put("message","编辑成功!");
                    return returnMap;
                }else{
                    returnMap.put("code","-1");
                    returnMap.put("message","部门/职务(" + changeIdToNameUtil.getNameByDepartId(dept) + "/" + changeIdToNameUtil.getNameByTitleId(titleid) + ")已经维护报销控制标准,不要重复维护!");
                    return returnMap;
                }
            }else{
                eaShipstandardMapper.updateById(eaShipstandard);

                returnMap.put("code","0");
                returnMap.put("message","编辑成功!");
                return returnMap;
            }
        }
    }
}
